<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 质量策划方案管理 -->
    <div id="base-breadcrumb" style="position: relative; overflow: hidden">
      <!-- 搜索 -->
      <searchData :searchData="searchArr" @search="search"></searchData>
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- <title-name :title="$route.meta.title"></title-name> -->
        <a-button v-btnPermission="'qm_001_add_01'" class="mb12" icon="plus" type="primary" @click="handleManage(2)">新增</a-button>
        <!-- 统计汇总 -->
        <AuditSummary :form="queryParam" :loadCount="loadCount" :api="api" />
        <!-- 表格 -->
        <list-table
          ref="table"
          tableLayout="fixed"
          size="default"
          rowKey="id"
          :columns="columns"
          :request="loadData"
          :alert="true"
          show-size-changer
          show-quick-jumper
          showPagination="auto"
        >
          <span slot="action" slot-scope="text, record">
            <MoreButton>
              <!-- 查看 -->
              <a v-btnPermission="'qm_001_loo_04'" @click="handleManage(0, record)">查看</a>
              <!-- 编辑 -->
              <template v-if="record.isOperation && (record.statusCode === '0' || record.statusCode === '3')">
                <a-divider v-btnPermission="'qm_001_edi_02'" type="vertical"></a-divider>
                <a v-btnPermission="'qm_001_edi_02'" @click="handleManage(1, record)">编辑</a>
              </template>
              <!-- 删除 -->
              <template v-if="record.isOperation && (record.statusCode === '0' && record.version === '1')">
                <a-divider v-btnPermission="'qm_001_del_03'" type="vertical"></a-divider>
                <a v-btnPermission="'qm_001_del_03'" @click="handleRemove(record)">删除</a>
              </template>
              <!-- 调整 -->
              <template v-if="record.statusCode === '2'">
                <a-divider v-btnPermission="'qm_001_rej_05'" type="vertical"></a-divider>
                <a v-btnPermission="'qm_001_rej_05'" @click="handleManage(3, record)">调整</a>
              </template>
              <!-- 当状态为审批中 -->
              <template v-if="record.statusCode != '0'">
                <a-divider v-btnPermission="'qm_001_exa_06'" type="vertical"></a-divider>
                <a v-btnPermission="'qm_001_exa_06'" @click="$bus.$emit('showApprovalDetailsDialog', record.businessId)">审批详情</a>
              </template>
            </MoreButton>
          </span>
          <!-- 版本 -->
          <a slot="version" slot-scope="text, record" @click="handleOpen(record)">{{
            record.version ? 'V' + record.version : ''
          }}</a>
          <!-- 状态 -->
          <template slot="statusCode" slot-scope="text, record">
            <status-display :statusCode="record.statusCode" isApproval></status-display>
          </template>
        </list-table>
      </a-card>
      <dialog-edit :visible.sync="visible" :api="api" :args="form"></dialog-edit>
    </div>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import { plotProgram } from '@/api/quality/quality' // api
import dialogEdit from '@/views/quality/components/VisionHistory' // 版本详情
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import Selector from '@/components/Select/Selector' // 状态
import SelectProjectStatus from '@/views/quality/components/SelectProjectStatus' // 项目状态
import OrgTreeSelect from '@/components/OrgTreeSelect'
import StatusDisplay from '@/components/StatusDisplay'
import AuditSummary from '@/views/quality/components/AuditSummary' // 统计汇总

const searchArr = [
  // 搜索框
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   type: SelectSubCompany,
  //   valueKey: 'branchCompanyId',
  // },
  // {
  //   name: '所属项目',
  //   type: SelectProject,
  //   valueKey: 'projectId',
  // },
  {
    name: '项目状态',
    type: SelectProjectStatus,
    valueKey: 'projectStatus',
  },
  {
    name: '策划人',
    type: 'input',
    value: 'planner',
  },
  {
    name: '策划时间',
    type: 'dateRange',
    value: 'planningDate',
    attr: {
      rangeFormat: ['startDate', 'endDate'],
    },
  },
  {
    name: '状态',
    valueKey: 'statusCode',
    type: Selector,
    selectType: '22',
    valueName:'code'
  },
]
const columns = [
  {
    title: '分公司',
    ellipsis: true,
    width: 180,
    dataIndex: 'branchCompanyName',
  },
  {
    title: '项目',
    ellipsis: true,
    width: 180, 
    dataIndex: 'projectName',
  },
  {
    title: '策划时间',
    ellipsis: true,
    width: 180, 
    dataIndex: 'planningDate',
  },
  {
    title: '策划人',
    ellipsis: true,
    width: 180,
    dataIndex: 'planner',
  },
  {
    title: '版本信息',
    dataIndex: 'version',
    width: 180,
    scopedSlots: { customRender: 'version' },
  },
  {
    title: '状态',
    dataIndex: 'statusCode',
    width: 180,
    scopedSlots: { customRender: 'statusCode' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 180,
    align: 'center',
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  name:'a'+ Date.now(),
  components: {
    STable,
    dialogEdit,
    StatusDisplay,
    // Selector,
    AuditSummary,
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      queryParam: {}, // 查询参数
      visible: false, // 弹窗显隐控件
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return plotProgram.postList(requestParameters).then((res) => {
          return res
        })
      },
      loadCount: 0, // 汇总加载控件
      form: {}, // 弹窗参数
    }
  },
  computed: {
    api: () => plotProgram,
  },
  methods: {
    /**
     * 搜索
     */
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
      this.loadCount++
    },
    /**
     * 查看历史版本
     */
    handleOpen(e) {
        this.form.id = e.id
        this.visible = true
    },
    /**
     * 删除草稿
     */
    handleRemove(record) {
      this.$confirm({
        title: '温馨提示',
        content: '确定删除草稿？',
        onOk: () => {
          return plotProgram
            .remove({ id: record.id })
            .then((res) => {
              this.$message.success('删除成功！')
              // 关闭当前页
              this.search()
            })
            // .catch((res) => {
            //   this.$message.warning('删除失败！')
            // })
        },
      })
    },
    /**
     * 操作栏
     */
    handleManage(type, record) {
      this.editModel = type
      let query = {
        editModel: type,
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: '/quality/plotProgram/plotProgramEdit',
        query,
      })
    },
  },
}
</script>
